package thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池
 * 是线程的管理
 * 1.重用
 * 2.控制数量
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(4);

        for(int i=0;i<5;i++){
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    Thread t = Thread.currentThread();
                    System.out.println(t.getName()+"开始执行");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(t.getName()+"执行完毕");
                }

            };
            executorService.execute(runnable);
            System.out.println("将一个任务交给线程");
        }

        executorService.shutdownNow();
        System.out.println("终止了");
    }
}
